package com.atguigu.gulimall.product.web;

import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.entity.service.CategoryService;
import com.atguigu.gulimall.product.vo.Catelog2Vo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class IndexController {
    @Autowired
    CategoryService categoryService;

    @GetMapping({"/", "/index.html"})
    public String indexPage(Model model) {
        //查出一级分类
        List<CategoryEntity> categoryEntities = categoryService.getlevel1Categorys();
        //
        model.addAttribute("categorys", categoryEntities);
        //视图解析器会拼串，前缀：“classpath:/templates/” 后缀：“.html”
        return "index";
    }

    @GetMapping("/index/json/catalog.json")
    @ResponseBody//返回json类型的数据,首页三级分类数据
    public Map<String, List<Catelog2Vo>> getCatalogJson() {
        Map<String, List<Catelog2Vo>> map = categoryService.getCatalogJson();
        return map;
    }

    //用于压测的简单服务
    @ResponseBody
    @GetMapping("/hello")
    public String hello() {
        return "hello";
    }

}
